package com.housediyadmin.view.titlewindow
{   	
	import com.housediyadmin.events.colorEvent.AddColorsEvent;
	import com.housediyadmin.model.ColorModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.vo.Color;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/15
	 */ 
	public class SelectColorMediator extends Mediator
	{
		[Inject]
		public var view:SelectColorView;
		
		[Inject]
		public var colorModel:ColorModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.ownColors, "dataProvider", colorModel, "addColors"); 
			view.previewBtn.addEventListener(MouseEvent.CLICK, prePopUpHandler);
			view.nextBtn.addEventListener(MouseEvent.CLICK, nextPopUpHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			view.addEventListener(CloseEvent.CLOSE, closeHandler);
			view.done.addEventListener(MouseEvent.CLICK, addColorHandler);
			view.ownColors.addEventListener(MouseEvent.CLICK, showColorInfoHandler);
		}
		
		private function addColorHandler(event:MouseEvent):void
		{
			var color:Color = new Color();
			color.colorName = view.colorNameTxt.text;
			color.colorNum = (view.colorNumTxt.text) as int;
			color.colorCreateDate = new Date();
			color.colorEditDate = new Date();
			color.goodsID = goodsModel.currentGoods.goodsID;
			if(view.ownColors.selectedItem == null)
			{
				colorModel.addColors.addItem(color);
			}
			else
			{
				var index:int = colorModel.addColors.getItemIndex(view.ownColors.selectedItem);
				colorModel.addColors.setItemAt(color, index);
			}
			
			
			
			view.colorNameTxt.text = null;
			view.colorNumTxt.text = null;
		}
		
		private function showColorInfoHandler(event:MouseEvent):void
		{
			var selectColor:Color = view.ownColors.selectedItem as Color;
			view.colorNameTxt.text = selectColor.colorName;
			view.colorNumTxt.text = (selectColor.colorNum) as String;
		}
		
		private function prePopUpHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:SelectSizeView = new SelectSizeView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function nextPopUpHandler(event:MouseEvent):void
		{
			dispatch(new AddColorsEvent(AddColorsEvent.ADD_COLORS, colorModel.addColors));
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:AppendGoodsPicView = new AppendGoodsPicView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function closeHandler(event:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
	}
}